package org.mysterylab.utopiaframework.web.servlet;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/**
 * 一个将动态 JSP 页面转成静态的 HTML 页面的 Servlet. <br>
 * 这种做法非常简单, 但是很麻烦, 一次只能给一个文件生成 html, 一般不使用该方法.
 * 
 * @author zhouych
 */
@SuppressWarnings("serial")
public class JspToHtmlServlet extends HttpServlet {

	public JspToHtmlServlet() {
		super();
	}

	@Override
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		processRequest(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		processRequest(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
	
	/**
	 * 处理的方法
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		ServletContext context = getServletContext();
		/*
		 * 访问时的文件名（不包括后缀名），作为参数提交给该servlet处理，比如：
		 * http://localhost/demo/JspToHtmlServlet?fileName=index
		 */
		String fileName = request.getParameter("fileName");
		String url = "/" + fileName + ".jsp";
		// 生成的Html文件名
		String htmlName = request.getContextPath() + fileName + ".html";
		RequestDispatcher dispatcher = context.getRequestDispatcher(url);
		final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		final ServletOutputStream servletOut = new ServletOutputStream() {
			
			@Override
			public void write(int b) throws IOException {
				byteOut.write(b);
			}

			@Override
			public void write(byte[] b, int off, int len) throws IOException {
				byteOut.write(b, off, len);
			}
		};
		final PrintWriter writer = new PrintWriter(new OutputStreamWriter(byteOut));
		HttpServletResponse res = new HttpServletResponseWrapper(response) {
			
			@Override
			public ServletOutputStream getOutputStream() {
				return servletOut;
			}
			
			@Override
			public PrintWriter getWriter() {
				return writer;
			}
		};
		dispatcher.include(request, res);
		writer.flush();
		// 把JSP输出的内容写到xxx.html
		FileOutputStream fileOut = new FileOutputStream(htmlName);
		byteOut.writeTo(fileOut);
		fileOut.close();
		response.getWriter().print("<p align=\"center\"><font size=\"3\" color=\"red\">页面成功生成！</font></p>");
	}
}
